/**
 * @file 传课.js 倾听传课提交验证
 */
/* globals vaildForm */
/* globals Popup */
/* globals tousu */
/* globals TipTog */
/* globals reloadPage */
/* globals getOS*/
/* globals getBrowser*/
var chkForm = {
    msg: {
        emailEmpty: '邮箱地址不能为空哦',
        emailErr: vaildForm.msg['emailErr'],
        contentEmpty: vaildForm.msg['contentEmpty'],
        contentLong: vaildForm.msg['contentLong'],
        phoneEmpty: '手机号码不能为空哦',
        phoneErr: vaildForm.msg['phoneErr']
    },
    appendUpimg: function (formIdNum) {
        var btnAdd = baidu('#form' + formIdNum + '_upimg_add');
        btnAdd.click(function () {
            var showId = btnAdd.attr('showid') * 1 + 1;
            baidu('#form' + formIdNum + '_upimg_' + showId).show();
            btnAdd.attr('showid', showId);
            if (showId === 4) {
                btnAdd.parent().hide();
            }
        });
    },
    checkFileType: function (file, tp) {
        var path = file.value;
        if (baidu.string.trim(path) === '') {
            return null;
        }

        var rlt = tp.test(path);
        return rlt;
    },
    setScreen: function (id) {
        var a = document.getElementById('form' + id + 'Screen');
        a.value = window.screen.width + 'x' + window.screen.height;
    }
};

var b = function () {
    var h = 'form1LogFile';
    var j = chkForm.checkFileType(document.getElementById(h), /\.txt$$|\.doc$$|\.docx$$/i);
    var i = '';
    if (j || (j === null)) {
        i = '';
    } else {
        i = '上传文件格式不正确';
    }
    vaildForm.setChkMsg(h, i);
    return(!i);
};
var checkFile = function (h) {
    var i = b();
    h = h && i;
    return h;
};
baidu('#form1LogFile').change(function () {
    var rlt = b();
    if (rlt) {
        var self = baidu(this);
        // btnWrapper.hide();
        baidu('#form1LogFile_ok').html(self.val()).show();
    }
});
baidu('#form1SelectFile').mouseover(function () {
    var bro = $.browser;
    if (bro.msie) {
        if (parseInt(bro.version, 10) < 8) {
            $('#form1LogFile').style.top = '142px';
            $('#form1LogFile').style.left = '185px';
        } else {
            $('#form1LogFile').style.top = this.offsetTop + 'px';
            $('#form1LogFile').style.left = (this.offsetLeft + 5) + 'px';
        }
    }
});
var c = function () {
    var h = 'form2LogFile';
    var j = chkForm.checkFileType(document.getElementById(h), /\.txt$$|\.doc$$|\.docx$$/i);
    var i = '';
    if (j || (j === null)) {
        i = '';
    } else {
        i = '上传文件格式不正确';
    }
    vaildForm.setChkMsg(h, i);
    return(!i);
};
var checkFile2 = function (h) {
    var i = c();
    h = h && i;
    return h;
};
baidu('#form2LogFile').change(function () {
    // c();
    var rlt = c();
    if (rlt) {
        var self = baidu(this);
        // btnWrapper.hide();
        baidu('#form2LogFile_ok').html(self.val()).show();
    }
});
baidu('#form2SelectFile').mouseover(function () {
    var bro = $.browser;
    if (bro.msie) {
        if (parseInt(bro.version, 10) < 8) {
            $('#form2LogFile').style.top = '142px';
            $('#form2LogFile').style.left = '185px';
        } else {
            $('#form2LogFile').style.top = this.offsetTop + 'px';
            $('#form2LogFile').style.left = (this.offsetLeft + 5) + 'px';
        }
    }
});

var form1 = {
    formO: baidu('#tousu_form1'),
    init: function () {
        var os = getOS();
        var browser = getBrowser();
        baidu('#form1_os').val(os);
        baidu('#form1_browser').val(browser);
        // 表单验证
        this.bindCheck();
        chkForm.appendUpimg(1);
        checkFile();
        chkForm.setScreen(1);
    },
    bindCheck: function () {
        var formO = this.formO;
        var formMsg = chkForm.msg;
        formO.vaildForm({
            fields: {
                '#form1_content': {
                    required: true,
                    regExp: 'len',
                    emptyMsg: formMsg['contentEmpty'],
                    errMsg: formMsg['contentLong']
                },
                '#form1_email': {
                    required: true,
                    regExp: 'email',
                    emptyMsg: formMsg['emailEmpty'],
                    errMsg: formMsg['emailErr']
                },
                '#form1_phone': {
                    required: true,
                    regExp: 'phone',
                    emptyMsg: formMsg['phoneEmpty'],
                    errMsg: formMsg['phoneErr']
                }
            }
        });
    }
};
var form2 = {
    formO: baidu('#tousu_form2'),
    init: function () {
        var os = getOS();
        var browser = getBrowser();
        baidu('#form2_os').val(os);
        baidu('#form2_browser').val(browser);
        // 表单验证
        this.bindCheck();
        chkForm.appendUpimg(2);
        checkFile2();
        chkForm.setScreen(2);
    },
    bindCheck: function () {
        var formO = this.formO;
        var formMsg = chkForm.msg;
        formO.vaildForm({
            fields: {
                '#form2_content': {
                    required: true,
                    regExp: 'len',
                    emptyMsg: formMsg['contentEmpty'],
                    errMsg: formMsg['contentLong']
                },
                '#form2_email': {
                    required: true,
                    regExp: 'email',
                    emptyMsg: formMsg['emailEmpty'],
                    errMsg: formMsg['emailErr']
                },
                '#form2_phone': {
                    required: true,
                    regExp: 'phone',
                    emptyMsg: formMsg['phoneEmpty'],
                    errMsg: formMsg['phoneErr']
                }
            }
        });
    }
};
form1.init();
form2.init();